package pri.hillchen.springbootstd.bms.config.mybatis;

import org.apache.ibatis.reflection.MetaObject;
import pri.hillchen.springbootstd.bms.common.util.ObjectUtil;

/**
 * 字段填充处理接口
 * Created by hillchen on 2017/10/20 0020.
 */
public interface FieldFillHandler {
    /**
     * 填充字段数据值
     *
     * @param metaObject
     */
    void fillField(String fieldName,MetaObject metaObject);

    /**
     * 校验是否存在指定字段
     * @param fieldName
     * @param metaObject
     * @return
     */
    default boolean hasField(String fieldName,MetaObject metaObject){
        return metaObject.hasGetter(fieldName)&&metaObject.hasSetter(fieldName);
    }

    default boolean needFill(String fieldName,MetaObject metaObject){
        if(fillWhenEmpty()){
            return ObjectUtil.isEmpty(metaObject.getValue(fieldName));
        }
        return true;
    }

    default boolean fillWhenEmpty(){
        return true;
    }
}
